package com.burstly.lib.component.networkcomponent.burstly.video;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.VideoView;
import com.burstly.lib.component.IBurstlyAdaptorListener;
import com.burstly.lib.component.networkcomponent.burstly.AudioControlReciever;
import com.burstly.lib.component.networkcomponent.burstly.BurstlyFullscreenActivity.IFullscreenView;
import com.burstly.lib.component.networkcomponent.burstly.BurstlyFullscreenConfiguration;
import com.burstly.lib.component.networkcomponent.burstly.FullscreenParams;
import com.burstly.lib.component.networkcomponent.burstly.FullscreenParams.VideoParams;
import com.burstly.lib.util.LoggerExt;
import com.burstly.lib.util.Utils;
import java.lang.ref.Reference;
import java.text.MessageFormat;

public final class VideoFullscreen
  implements BurstlyFullscreenActivity.IFullscreenView, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnInfoListener, MediaPlayer.OnSeekCompleteListener
{
  public static final Integer IMPLEMENTATION_ID = Integer.valueOf(3);
  private static final LoggerExt LOG = LoggerExt.getInstance();
  static final int SECOND_IN_MILLIS = 1000;
  private static final int SKIP_BUTTON_ID = 3;
  private static final String SKIP_VIDEO_MESSAGE = "You can skip this video in {0} seconds";
  private static final String TAG = VideoFullscreen.class.getSimpleName();
  private static final int TOOL_BAR_ID = 1;
  static final String VIDEO_URL = "videoUrl";
  private static final int VIDEO_VIEW_ID = 4;
  private static final int VISIT_BUTTON_ID = 2;
  public static volatile boolean sIsShowingVideoNow;
  Activity mActivity;
  private AudioControlReciever mAudioControl;
  private Runnable mCountdownRunnable;
  private ProgressDialog mDialog;
  Handler mHandler;
  private int mHideToolbarAfter;
  volatile boolean mIsShowing;
  volatile boolean mIsTimedOut;
  volatile boolean mIsToolbarShowing;
  private FullscreenParams.VideoParams mParams;
  private int mPosition;
  private VideoTracker mProgressWatcher;
  private FrameLayout mRoot;
  private float mScaleFactor;
  Integer mShowSkipButtonAfter;
  private ImageView mSkipButton;
  private final Runnable mTimeout = new Runnable()
  {
    public void run()
    {
      VideoFullscreen.LOG.logDebug(VideoFullscreen.TAG, "Video loading timed out! Closing video...", new Object[0]);
      VideoFullscreen.this.mIsTimedOut = true;
      try
      {
        VideoFullscreen.this.closeLoadingDialog();
        VideoFullscreen.this.mActivity.finish();
        return;
      }
      catch (Exception localException)
      {
        VideoFullscreen.LOG.logError(VideoFullscreen.TAG, Log.getStackTraceString(localException), new Object[0]);
      }
    }
  };
  private int mToolBarHeight;
  private RelativeLayout mToolbar;
  private String mUrl;
  private VideoView mVideo;
  private ImageView mVisitButton;
  boolean mVisitWasClicked;
  private TextView mWaitMessageView;

  private void closeLoadingDialog()
  {
    if ((this.mDialog != null) && (this.mDialog.isShowing()))
      this.mDialog.dismiss();
  }

  private void createLoadingDialog()
  {
    this.mDialog = new ProgressDialog(this.mActivity, 16973840);
    this.mDialog.setCancelable(true);
    this.mDialog.setMessage("Loading video...");
    this.mDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
    {
      public void onCancel(DialogInterface paramAnonymousDialogInterface)
      {
        paramAnonymousDialogInterface.dismiss();
        VideoFullscreen.this.stopTimeoutWatching();
        VideoFullscreen.this.mActivity.finish();
      }
    });
    this.mDialog.setIndeterminate(true);
    this.mDialog.show();
  }

  private void createSkipButton()
  {
    this.mSkipButton = new ImageView(this.mActivity);
    this.mSkipButton.setId(3);
    this.mSkipButton.setPadding(0, 0, 0, 0);
    BitmapDrawable localBitmapDrawable = (BitmapDrawable)Utils.getSkipImage(this.mActivity);
    this.mSkipButton.setImageDrawable(localBitmapDrawable);
    this.mSkipButton.setVisibility(8);
    RelativeLayout.LayoutParams localLayoutParams = getLayoutParameters(localBitmapDrawable);
    localLayoutParams.addRule(11);
    this.mSkipButton.setLayoutParams(localLayoutParams);
    this.mSkipButton.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        if (VideoFullscreen.this.mIsToolbarShowing)
        {
          VideoFullscreen.this.mActivity.finish();
          return;
        }
        VideoFullscreen.this.showToolbar();
      }
    });
    showCloseButtonDelayMessage();
  }

  private void createToolBar()
  {
    createToolBarLayout();
    if (this.mParams.isShowVisitButton())
    {
      createVisitButton();
      this.mToolbar.addView(this.mVisitButton);
    }
    createSkipButton();
    this.mToolbar.addView(this.mSkipButton);
    this.mRoot.addView(this.mToolbar);
    this.mRoot.bringChildToFront(this.mToolbar);
  }

  @TargetApi(16)
  private void createToolBarLayout()
  {
    this.mToolbar = new RelativeLayout(this.mActivity);
    this.mToolbar.setId(1);
    this.mToolbar.setVisibility(4);
    if (Build.VERSION.SDK_INT >= 16)
      this.mToolbar.setBackground(Utils.getToolbarImage(this.mActivity));
    while (true)
    {
      this.mToolbar.setLayoutParams(new FrameLayout.LayoutParams(Utils.getFillparentValue(), this.mToolBarHeight, 80));
      return;
      this.mToolbar.setBackgroundDrawable(Utils.getToolbarImage(this.mActivity));
    }
  }

  private void createVisitButton()
  {
    this.mVisitButton = new ImageView(this.mActivity);
    this.mVisitButton.setId(2);
    this.mVisitButton.setAdjustViewBounds(true);
    this.mVisitButton.setPadding(0, 0, 0, 0);
    BitmapDrawable localBitmapDrawable = (BitmapDrawable)Utils.getVisitSiteImage(this.mActivity);
    this.mVisitButton.setImageDrawable(localBitmapDrawable);
    RelativeLayout.LayoutParams localLayoutParams = getLayoutParameters(localBitmapDrawable);
    localLayoutParams.addRule(9);
    this.mVisitButton.setLayoutParams(localLayoutParams);
    this.mVisitButton.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        if (VideoFullscreen.this.mIsToolbarShowing)
        {
          Reference localReference = VideoFullscreen.this.mParams.getVideoComponent();
          if (localReference != null);
          for (BurstlyVideoAdaptor localBurstlyVideoAdaptor = (BurstlyVideoAdaptor)localReference.get(); ; localBurstlyVideoAdaptor = null)
          {
            if (localBurstlyVideoAdaptor != null)
            {
              localBurstlyVideoAdaptor.getAdaptorListener().adWasClicked("VideoInterstitial", true);
              VideoFullscreen.this.mVisitWasClicked = true;
            }
            return;
          }
        }
        VideoFullscreen.this.showToolbar();
      }
    });
  }

  private float getImageHeightFactor(float paramFloat)
  {
    return this.mToolBarHeight / paramFloat;
  }

  private RelativeLayout.LayoutParams getLayoutParameters(BitmapDrawable paramBitmapDrawable)
  {
    int i = paramBitmapDrawable.getBitmap().getWidth();
    return new RelativeLayout.LayoutParams((int)(getImageHeightFactor(paramBitmapDrawable.getBitmap().getHeight()) * i), this.mToolBarHeight);
  }

  private void init()
  {
    this.mScaleFactor = Utils.getScale(this.mActivity);
    this.mToolBarHeight = ((int)(48.0F * this.mScaleFactor));
    this.mHandler = new Handler(this.mActivity.getMainLooper());
    this.mUrl = this.mActivity.getIntent().getExtras().getString("videoUrl");
    this.mParams = BurstlyFullscreenConfiguration.getFullscreenParams(this.mUrl).getVideoParams();
    this.mHideToolbarAfter = this.mParams.getHideToolbarAfter();
    this.mShowSkipButtonAfter = Integer.valueOf(this.mParams.getShowSkipButtonAfter());
    this.mProgressWatcher = new VideoTracker(this.mVideo, this.mParams.getMiddleVideoCallback());
    BurstlyFullscreenConfiguration.removeFullscreenParams(this.mUrl);
  }

  private boolean isValidActvityState()
  {
    Intent localIntent = this.mActivity.getIntent();
    boolean bool = true;
    if ((localIntent == null) || (localIntent.getExtras() == null))
    {
      LOG.logWarning(TAG, "Intent is NULL or extras are NULL", new Object[0]);
      bool = false;
    }
    String str;
    FullscreenParams localFullscreenParams;
    do
    {
      return bool;
      str = localIntent.getExtras().getString("videoUrl");
      localFullscreenParams = BurstlyFullscreenConfiguration.getFullscreenParams(str);
      if (localFullscreenParams == null)
      {
        LOG.logWarning(TAG, "No fullscreen parameters for url: " + str, new Object[0]);
        bool = false;
      }
    }
    while (localFullscreenParams.getVideoParams() != null);
    LOG.logWarning(TAG, "No video parameters for url: " + str, new Object[0]);
    return false;
  }

  private void resumeOrStartWatcher()
  {
    if (!this.mParams.isPrepared())
    {
      this.mProgressWatcher.resume();
      return;
    }
    LOG.logDebug(TAG, "Starting video progress watcher on precached video...", new Object[0]);
    this.mProgressWatcher.startMiddlepointTracking();
  }

  private static void setPopupToolbar(ViewGroup paramViewGroup)
  {
    AnimationSet localAnimationSet = new AnimationSet(true);
    localAnimationSet.addAnimation(new AlphaAnimation(0.0F, 1.0F));
    localAnimationSet.addAnimation(new TranslateAnimation(1, 0.0F, 1, 0.0F, 1, 1.0F, 1, 0.0F));
    localAnimationSet.setDuration(500L);
    localAnimationSet.setFillAfter(true);
    paramViewGroup.startAnimation(localAnimationSet);
  }

  private static void setPullDownToolbar(ViewGroup paramViewGroup)
  {
    AnimationSet localAnimationSet = new AnimationSet(true);
    localAnimationSet.addAnimation(new AlphaAnimation(1.0F, 0.0F));
    localAnimationSet.addAnimation(new TranslateAnimation(1, 0.0F, 1, 0.0F, 1, 0.0F, 1, 1.0F));
    localAnimationSet.setDuration(500L);
    localAnimationSet.setFillAfter(true);
    paramViewGroup.startAnimation(localAnimationSet);
  }

  private void showCloseButtonDelayMessage()
  {
    if ((this.mShowSkipButtonAfter != null) && (this.mShowSkipButtonAfter.intValue() > 0))
    {
      this.mWaitMessageView = new TextView(this.mActivity);
      TextView localTextView = this.mWaitMessageView;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = Integer.valueOf(this.mShowSkipButtonAfter.intValue() / 1000);
      localTextView.setText(MessageFormat.format("You can skip this video in {0} seconds", arrayOfObject));
      this.mWaitMessageView.setTextSize(1, 13.0F);
      RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(-2, -2);
      localLayoutParams.addRule(11);
      localLayoutParams.addRule(15);
      localLayoutParams.rightMargin = 15;
      this.mWaitMessageView.setLayoutParams(localLayoutParams);
      this.mToolbar.addView(this.mWaitMessageView);
      return;
    }
    this.mSkipButton.setVisibility(0);
  }

  private void showToolbar()
  {
    this.mToolbar.setVisibility(0);
    setPopupToolbar(this.mToolbar);
    this.mIsToolbarShowing = true;
    this.mHandler.postDelayed(new Runnable()
    {
      public void run()
      {
        if (VideoFullscreen.this.mIsShowing)
        {
          VideoFullscreen.setPullDownToolbar(VideoFullscreen.this.mToolbar);
          VideoFullscreen.this.mToolbar.setVisibility(4);
          VideoFullscreen.this.mIsToolbarShowing = false;
        }
      }
    }
    , this.mHideToolbarAfter);
  }

  private void startCountdownToAllowSkip()
  {
    if ((this.mShowSkipButtonAfter != null) && (this.mShowSkipButtonAfter.intValue() > 0))
    {
      this.mCountdownRunnable = new Runnable()
      {
        public void run()
        {
          Integer localInteger = Integer.valueOf(-1000 + VideoFullscreen.this.mShowSkipButtonAfter.intValue());
          if ((localInteger.intValue() > 0) && (VideoFullscreen.this.mShowSkipButtonAfter.intValue() > 0))
          {
            VideoFullscreen.this.mShowSkipButtonAfter = localInteger;
            Object[] arrayOfObject = new Object[1];
            arrayOfObject[0] = Integer.valueOf(VideoFullscreen.this.mShowSkipButtonAfter.intValue() / 1000);
            String str = MessageFormat.format("You can skip this video in {0} seconds", arrayOfObject);
            VideoFullscreen.this.mWaitMessageView.setText(str);
            VideoFullscreen.this.mHandler.postDelayed(this, 1000L);
            return;
          }
          VideoFullscreen.this.mShowSkipButtonAfter = Integer.valueOf(0);
          VideoFullscreen.this.mWaitMessageView.setVisibility(8);
          Animation localAnimation1 = AnimationUtils.loadAnimation(VideoFullscreen.this.mActivity.getApplicationContext(), 17432577);
          VideoFullscreen.this.mWaitMessageView.startAnimation(localAnimation1);
          VideoFullscreen.this.mSkipButton.setVisibility(0);
          Animation localAnimation2 = AnimationUtils.loadAnimation(VideoFullscreen.this.mActivity.getApplicationContext(), 17432576);
          VideoFullscreen.this.mSkipButton.startAnimation(localAnimation2);
        }
      };
      this.mHandler.postDelayed(this.mCountdownRunnable, 1000L);
    }
  }

  private void stopTimeoutWatching()
  {
    if (!this.mParams.isPrepared())
    {
      LOG.logInfo(TAG, "Video watchdog stopped.", new Object[0]);
      this.mHandler.removeCallbacks(this.mTimeout);
    }
  }

  private void watch()
  {
    if (!this.mParams.isPrepared())
    {
      LOG.logInfo(TAG, "Starting video load watchdog...", new Object[0]);
      this.mHandler.postDelayed(this.mTimeout, 10000L);
    }
  }

  void adjustLayout()
  {
    this.mRoot.invalidate();
    this.mRoot.requestLayout();
    this.mToolbar.invalidate();
    this.mToolbar.requestLayout();
  }

  public ViewGroup.LayoutParams getAttachParameters()
  {
    return null;
  }

  public void onCompletion(MediaPlayer paramMediaPlayer)
  {
    if (!this.mIsTimedOut)
    {
      LoggerExt localLoggerExt = LOG;
      String str = TAG;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = this.mUrl;
      localLoggerExt.logInfo(str, "Completed playing video file: {0}", arrayOfObject);
      LOG.logInfo(TAG, "Sending watch-to-the-end track request...", new Object[0]);
      VideoTracker.trackVideoEnd(this.mParams.getEndVideoCallback());
      this.mActivity.finish();
    }
  }

  public View onCreate(Bundle paramBundle, Activity paramActivity)
  {
    this.mActivity = paramActivity;
    this.mRoot = new FrameLayout(paramActivity);
    this.mVideo = new VideoView(paramActivity);
    if (!isValidActvityState())
      return null;
    init();
    watch();
    this.mIsShowing = true;
    if (!this.mParams.isPrepared())
      createLoadingDialog();
    this.mAudioControl = new AudioControlReciever(paramActivity);
    this.mVideo.setId(4);
    this.mVideo.setKeepScreenOn(true);
    this.mVideo.requestFocus();
    this.mVideo.setOnCompletionListener(this);
    this.mVideo.setOnPreparedListener(this);
    this.mVideo.setOnErrorListener(this);
    int i = Utils.getFillparentValue();
    this.mRoot.addView(this.mVideo, new FrameLayout.LayoutParams(i, i, 17));
    createToolBar();
    return this.mRoot;
  }

  public void onDestroy(Activity paramActivity)
  {
    sIsShowingVideoNow = false;
    if (this.mProgressWatcher != null)
    {
      this.mProgressWatcher.cancel();
      this.mProgressWatcher = null;
    }
    if (this.mCountdownRunnable != null)
      this.mHandler.removeCallbacks(this.mCountdownRunnable);
    if ((!this.mVisitWasClicked) && (BurstlyVideoAdaptor.sCurrentInstance != null))
      BurstlyVideoAdaptor.sCurrentInstance.getAdaptorListener().dismissedFullscreen(BurstlyVideoAdaptor.FULLSCREEN_INFO);
  }

  public boolean onError(MediaPlayer paramMediaPlayer, int paramInt1, int paramInt2)
  {
    if (!this.mIsTimedOut)
    {
      stopTimeoutWatching();
      this.mProgressWatcher.cancel();
      LoggerExt localLoggerExt = LOG;
      String str = TAG;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = this.mUrl;
      localLoggerExt.logWarning(str, "Error while playing video file: {0}", arrayOfObject);
      this.mActivity.finish();
    }
    return true;
  }

  public boolean onInfo(MediaPlayer paramMediaPlayer, int paramInt1, int paramInt2)
  {
    LoggerExt localLoggerExt = LOG;
    String str = TAG;
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = this.mUrl;
    localLoggerExt.logWarning(str, "Info playing video file: {0}", arrayOfObject);
    return false;
  }

  public boolean onKey(Activity paramActivity, int paramInt, KeyEvent paramKeyEvent)
  {
    return false;
  }

  @TargetApi(8)
  public void onPause(Activity paramActivity)
  {
    this.mIsShowing = false;
    this.mPosition = this.mVideo.getCurrentPosition();
    this.mVideo.stopPlayback();
    this.mProgressWatcher.pause();
    this.mAudioControl.unRegisterReciever();
    closeLoadingDialog();
    if (this.mCountdownRunnable != null)
      this.mHandler.removeCallbacks(this.mCountdownRunnable);
    if (this.mVisitWasClicked)
      paramActivity.finish();
  }

  public void onPrepared(MediaPlayer paramMediaPlayer)
  {
    if (!this.mIsTimedOut)
    {
      this.mVideo.start();
      this.mAudioControl.setMediaPlayer(paramMediaPlayer);
      this.mAudioControl.registerReciever();
      paramMediaPlayer.setOnSeekCompleteListener(this);
      if (this.mPosition != 0)
        this.mVideo.seekTo(this.mPosition);
      stopTimeoutWatching();
      if (!this.mParams.isPrepared())
      {
        LOG.logDebug(TAG, "Starting video progress watcher on unprecached video...", new Object[0]);
        this.mProgressWatcher.startMiddlepointTracking();
      }
      adjustLayout();
      if (!this.mIsToolbarShowing)
        this.mHandler.postDelayed(new Runnable()
        {
          public void run()
          {
            VideoFullscreen.this.adjustLayout();
            VideoFullscreen.this.showToolbar();
          }
        }
        , 1000L);
      closeLoadingDialog();
      if ((this.mShowSkipButtonAfter != null) && (this.mShowSkipButtonAfter.intValue() > 0))
        startCountdownToAllowSkip();
    }
  }

  @TargetApi(8)
  public void onResume(Activity paramActivity)
  {
    this.mIsShowing = true;
    resumeOrStartWatcher();
    this.mVideo.setVideoPath(this.mUrl);
  }

  public void onSeekComplete(MediaPlayer paramMediaPlayer)
  {
    this.mVideo.start();
  }

  public boolean onTouchEvent(Activity paramActivity, MotionEvent paramMotionEvent)
  {
    if (!this.mIsToolbarShowing)
      showToolbar();
    return true;
  }

  public boolean showDefaultCloseButton()
  {
    return false;
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.component.networkcomponent.burstly.video.VideoFullscreen
 * JD-Core Version:    0.6.2
 */